package com.dwk.rabbitMQ4;

import com.dwk.utils.RabbitMqUtils;
import com.dwk.utils.SleepUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

public class Work03_02 {
    private static final String TASK_QUEUE_NAME="durable_ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C2等待接收消息...长时间处理");

        DeliverCallback deliverCallback=(consumerTag, message) ->{
            SleepUtils.sleep(30);
            System.out.println(new String(message.getBody(),"UTF-8"));
            //手动应答
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };

        CancelCallback cancelCallback= var1 ->{
            System.out.println("消息消费被中断");
        };
        //设置不公平分发
        int prefetchCount=1;
        channel.basicQos(prefetchCount);
        //采用手动应答
        boolean autoAck =false;
        channel.basicConsume(TASK_QUEUE_NAME,autoAck,deliverCallback,cancelCallback );
    }
}
